<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;

	//后台主页
	class Nav extends Allow
	{
    public function getindex()
    {
      //拿到List表全部数据
            $data=Db::query("select *,concat(path,',',id) as paths from nav order by paths;");
      foreach($data as $key=>$value){
        $num=count(explode(',',$value['path']))-1;
        $data[$key]['name']=str_repeat('--|',$num).$value['name'];
      }
            //分配数据
      return $this->fetch('nav/index',['data'=>$data]);
    }

		public function getAdd()
		{
			//获取ID
			$request=request();
			$id=$request->param('id');
			//查询数据
			$list=Db::table('nav')->where('id',$id)->select();
            //分配模板
		    return $this->fetch('nav/add',['list'=>$list]);
	    }
	    public function postInsert()
	    {
	    	//获取参数
	    	$request=request();
        if($request->param('name')=="")
        {
          $this->error('请填写完整','/nav/index');
        }

            //获取path
            $path=$request->param('path');
            //获取PID
            $pid=$request->param('pid');
            //合成新的path
            $newpath=$path.','.$pid;
            //挑选参数(list)
            $data=$request->except(['path','action']);
            //将新的path插入
            $data['path']=$newpath;
            //如果URL等于空那不是单网页
            if($request->param('url')=="")
            {
              $data['urls']=1;
            }else
            {
              $data['urls']=2;
            }
            //执行添加到nav表
            $list=Db::table('nav')->insert($data);
            if($list){
            	$this->success('添加成功','/nav/index');
            }else{
            	$this->error('添加失败','/nav/index');
            }
          }
       public function getEdit()
       {
       	//获取pid与ID
         $request=request();

         $id=$request->param('id');
         //根据id获取数据
         $list=Db::table('nav')->where('id',$id)->find();
         //逗号来分割字符串
        $a=explode(',',$list['path']);
        //移除最后一个元素
        array_pop($a);
        //将数组转化为字符串
        $b=implode(',',$a);
         //获取所有数据父类集
         $data=Db::table('nav')->where('id','<>',$id)->where('path',$b)->select();
         //判断是否有子集
         $result=DB::table('nav')->where('pid',$id)->select();
         //加载模板
         return $this->fetch('nav/edit',['list'=>$list,'data'=>$data,'result'=>$result]);
       }
       public function postupdate()
       {
        $request=request();
         if($request->param('name')=="")
         {
          $this->error('请填写完整','/nav/index');
          exit;
         }
        $id=$request->param('id');
        $data=$request->except(['action']);
        $list=Db::table('nav')->where('id',$id)->update($data);
        if($list)
        {
          $this->success('修改成功','/nav/index');
        }else{
          $this->error('修改失败','/nav/index');
        }
       
       }
       public  function getdelete()
       {
        $request=request();
        $id=$request->param('id');
        //判断是否有子栏目
        $data=Db::table('nav')->where('pid',$id)->select();
        if(!empty($data))
        {
            $this->error('请先删除该栏目的子栏目','/nav/index');
            exit;
        }else
        {
          //没有子集执行删除
         $list=Db::table('nav')->where('id',$id)->delete();
         if($list)
         {
          $this->success('删除成功','/nav/index');
         }else
         {
          $this->error('删除失败','/nav/index');
         }
        }
       }
       //顶级添加
       public function getadds()
       {
       
        return $this->fetch('nav/adds');
       }
       public function postinserts()
       {

        //获取参数
            $request=request();
            if($request->param('name')=="")
            {
              $this->error('请填写完整','/nav/adds');
              exit; 
            }
            //挑选参数
            $data=$request->except(['action']);
            if($request->param('url')=="")
            {
              $data['urls']=1;
            }else
            {
              $data['urls']=2;
            }
            //path为0
            $data['path']=0;
            //pid为0
             $data['pid']=0;
            //执行添加
            $list=Db::table('nav')->insert($data);
            if($list){
              $this->success('添加成功','/nav/index');
            }else{
              $this->error('添加失败','/nav/index');
            }
          
       }
       public function getessay()
       {
        $request=request();
        $id=$request->param('id');
        $list=Db::table('nav')->where('id',$id)->find();
        return $this->fetch('nav/essay',['list'=>$list,'id'=>$id]);
       }
       public function postessayadd()
       {
       
         $request=request();
         $id=$request->param('id');
         $data=$request->except(['action']);
         $list=Db::table('nav')->where('id',$id)->update($data);
         if($list)
         {
          $this->success('修改成功','/nav/index');
         }else
         {
          $this->error('修改失败','/nav/index');
         }
      
    

       }
		

	}
 ?>